<template>
  <div class="login">
    <van-image width="100%" height="220" :src="picurl" />
    <van-button type="primary" class='bottom padding' @click="backHome()">返回首页</van-button>
    <van-button type="primary" class='bottom' open-type="getUserInfo" @getuserinfo="bindGetUserInfo">微信用户一键登录</van-button>
    <!-- <div class="login-account">
      <navigator url="../loginAccount/index">手机号验证注册/登录</navigator>
    </div> -->
  </div>
</template>

<script>
import store from "@/utils/store.js";
import utils from "@/utils/index.js";

export default {
  config: {
    navigationBarTitleText: "登录"
  },
  data() {
    return {
      user: { nickName: "立即登录" }
    };
  },
  onLoad: function () {
    let that = this;
    wx.setNavigationBarTitle({
      title: "登录"
    })
  },
  computed: {
    picurl(){
      let picurl = utils.fileMgrUrl + '55c9cb02-ae58-4b84-84c2-8c1876d94283.jpg'
      return picurl
    }
  },
  methods: {
    bindGetUserInfo(e) {
      if (e.mp.detail.userInfo) {
        var that = this;
        console.log("用户信息", e.mp.detail.userInfo)
        that.$API.post("/user/upDataUserInfo?name=" +
          e.mp.detail.userInfo.nickName +
          "&headUrl=" +
          e.mp.detail.userInfo.avatarUrl
        ).then(resp => {
        })
        let user = store.state.user
        user.nickName = e.mp.detail.userInfo.nickName
        user.wechatHeadImgUrl = e.mp.detail.userInfo.avatarUrl
        store.commit("setUser", user);
        wx.navigateBack();
      } else {
        console.log("请授权后继续操作。", e.mp.detail)
      }
    },
    wxLoginBtn() {
      var that = this
      wx.login({
        success: loginResp => {
          that.$API.post("wechat/wxLogin?code=" + loginResp.code).then(res => {
            store.commit("setToken", res.data.token);
            store.commit("setUser", res.data.user);
            console.log("登录成功");
            wx.navigateBack({ delta: 2 });
          });
        },
        fail: res => {
          // do not thing
          // this.toLogin();

        }
      });
    },
    backHome() {
      const url = "/pages/index/index";
      wx.switchTab({ url });
    }
  }
};
</script>


<style>
.login {
  margin: 5px;
  text-align: center;
}
.login-account {
  padding-top: 15px;
}
  .padding {
    padding-right: 10px;
  }
</style>
